<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <!-- <el-form-item
        label="用户id"
        prop="userId"
      >
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="岗位"
        prop="work"
      >
        <el-input
          v-model="queryParams.work"
          placeholder="请输入岗位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置</el-button>
      </el-form-item> -->
    </el-form>

    <el-row
      :gutter="10"
      class="mb8"
    >
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:user:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:user:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:user:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="userList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      />
      <el-table-column
        label="序号"
        align="center"
        prop="id"
      > <template slot-scope="scope">
          <span>{{(queryParams.pageNum-1)*queryParams.pageSize+(scope.$index + 1)}} </span>
        </template>
      </el-table-column>
      <el-table-column
        label="公司"
        align="center"
        prop="deptName"
      />
      <el-table-column
        label="用户"
        align="center"
        prop="userName"
      />
      <el-table-column
        label="手机号"
        align="center"
        prop="telephone"
      />
      <el-table-column
        label="岗位"
        align="center"
        prop="work"
      />
      <el-table-column
        label="就餐类型"
        align="center"
        prop="diningName"
      />
      <el-table-column
        label="免费时间"
        align="center"
        prop="freeTime"
      />
      <el-table-column
        label="是否取餐"
        align="center"
        prop="isTakeFood"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.isTakeFood == 0">未取</span>
          <span v-if="scope.row.isTakeFood == 1">已取</span>
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-if="false"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:user:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:user:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改免费就餐人员信息管理对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item
          label="用户电话"
          prop="telephone"
        >
          <el-input
            v-model="form.telephone"
            placeholder="请输入订餐用户电话号码"
            @change="getUserInfo"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="公司名称"
          prop="companyId"
        >
          <el-select
            v-model="form.deptId"
            placeholder="请输入公司名称"
            :disabled="disabled.add"
          >
            <el-option
              v-for="i in companyInfos"
              :key="i.deptId"
              :label="i.deptName"
              :value="i.deptId"
            ></el-option>
          </el-select>
<!--          <el-select-->
<!--            v-model="form.companyId"-->
<!--            placeholder="请选择公司名称"-->
<!--            clearable-->
<!--            size="small"-->
<!--            :disabled="disabled.add"-->
<!--          >-->
<!--            <el-option-->
<!--              v-for="item in companyInfos"-->
<!--              :key="item.id"-->
<!--              :label="item.text"-->
<!--              :value="Number(item.id)"-->
<!--            ></el-option>-->
<!--          </el-select>-->
        </el-form-item>

        <el-form-item
          label="用户ID"
          prop="userId"
          v-if="false"
        >
          <el-input
            v-model="form.userId"
            :disabled="disabled.add"
            placeholder="请输入用户名称"
          />
        </el-form-item>
        <el-form-item
          label="岗位"
          prop="work"
        >
          <el-input
            v-model="form.work"
            :disabled="disabled.add"
            placeholder="请输入岗位"
          />
        </el-form-item>
        <el-form-item
          label="就餐类型"
          :disabled="disabled.add"
          prop="diningType"
        >
          <el-select
            multiple
            v-model="form.diningType"
            :disabled="disabled.add"
            placeholder="请选择就餐类型"
            style="width: 100%"
            clearable
          >
            <el-option
              label="早餐"
              value="1"
            />
            <el-option
              label="午餐"
              value="2"
            />
            <el-option
              label="晚餐"
              value="3"
            />
            <el-option
              label="接待餐"
              value="4"
            />
<!--            <el-option-->
<!--              label="其它"-->
<!--              value="5"-->
<!--            />-->
          </el-select>
        </el-form-item>

        <el-form-item
          label="免费时间"
          prop="freeTime"
        >
          <el-date-picker
            v-model="form.freeTime"
            type="date"
            value-format="yyyy-MM-dd"
            :disabled="disabled.add"
            placeholder="请选择免费时间"
            :picker-options="expireTimeOPtion"
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          :disabled="disabled.add"
          @click="submitForm"
        >确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listUser,
  getUser,
  delUser,
  addUser,
  updateUser,
  exportUser,
  checkUserInfo,
} from "@/api/freeFood/FoodUser";
import { Col } from "element-ui";

// import { listUser} from '@/api/Recharge/user.js';
export default {
  name: "User",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 免费就餐人员信息管理表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        work: undefined,
        diningType: undefined,
        freeTime: undefined,
        isTakeFood: undefined,
      },
      companyList: [],
      companyInfos: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        telephone: [
          {
            required: true,
            message: `请输入订餐用户电话号码`,
            trigger: "change",
          },
        ],
        companyId: [
          { required: true, message: `请输入岗位`, trigger: "change" },
        ],
        work: [{ required: true, message: `请输入岗位`, trigger: "change" }],
        diningType: [
          { required: true, message: `请选择就餐类型`, trigger: "change" },
        ],
        freeTime: [
          { required: true, message: `请选择免费时间`, trigger: "change" },
        ],
      },
      // 表单禁填
      disabled: {
        add: false,
        edit: false,
      },
      expireTimeOPtion: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7;
        },
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询免费就餐人员信息管理列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then((response) => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取 电话 用户信息
    getUserInfo(val) {
      checkUserInfo(val).then((res) => {
        console.log(`phone:`, res);
        this.disabled.add = !res.data;

        if (res.data) {
          let companyId;
          let { id, telephone, sysDeptList} = res.data;
          console.log(`sysDeptList:`, sysDeptList)
          this.companyInfos = sysDeptList;
          if (res.data.sysDeptList.length) {
            let { deptId } = res.data.sysDeptList[0];
            companyId = deptId;
          }
          this.form = { userId: id, telephone, companyId };
          console.log("公司信息:", this.form);
        } else {
          this.msgError("该用户不存在, 禁止填写信息！");
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.disabled.add = false;
      this.companyInfos = [];
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,
        work: undefined,
        diningType: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
        freeTime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加免费就餐人员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getUser(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改免费就餐人员";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // if (this.form.id != undefined) {
          //     updateUser(this.form).then((response) => {
          //         if (response.code === 200) {
          //             this.msgSuccess("修改成功");
          //             this.open = false;
          //             this.getList();
          //         }
          //     });
          // } else {
          let _form = this.form;
          _form.diningType = this.form.diningType.join("");
          _form.companyId = this.form.deptId;
          console.log(`_form:`, _form);
          addUser(_form).then((response) => {
            this.msgSuccess("新增成功");
            this.open = false;
            this.companyInfos = [];
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除免费就餐人员信息管理编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delUser(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有免费就餐人员信息管理数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportUser(queryParams);
        })
        .then((response) => {
          this.download(response.data);
        })
        .catch(function () {});
    },
  },
};
</script>
